package com.plian.system.task.rectificationSituation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.plian.Tools.StringUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.sys.user.UserDao;
import com.plian.system.entity.ms.RectificationSituation;

import com.plian.system.entity.sys.UserOrg;
import com.plian.system.entity.taskMsg.ScheduledTasksMsg;


import com.plian.system.mapper.tasksMsg.ScheduledTasksMsgMapper;
import com.plian.system.service.ms.IRectificationSituationService;
import com.plian.system.service.sys.user.IUserOrgService;
import com.plian.system.service.tasksMsg.IScheduledTasksMsgService;

import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**定时弹窗
 * @Author：zsk
 * @Package：com.plian.system.task.rectificationSituation
 * @Project：xt-gzw
 * @name：RectificationSituationTask
 * @Date：2025/2/17 9:55
 * @Filename：RectificationSituationTask
 */
@Configuration
public class RectificationSituationTask {
    private static final Logger log = LoggerFactory.getLogger(RectificationSituationTask.class);
    @Resource
    private IRectificationSituationService iRectificationSituationService;
    @Resource
    private IScheduledTasksMsgService iScheduledTasksMsgService;
    @Resource
    private UserDao userDao;
    @Resource
    private IUserOrgService iUserOrgService;
    private String moduleType="zgqk";
    @Resource
    private ScheduledTasksMsgMapper scheduledTasksMsgMapper;
    @Scheduled(cron = "0 0 0 ? * ? ")
    private void configureTasks() {
        QueryWrapper<RectificationSituation> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("rectification_situation","124001");//获取未整改完成的整改情况数据
        queryWrapper.eq("is_deleted",0);
        List<RectificationSituation> list=iRectificationSituationService.list(queryWrapper);
        QueryWrapper<ScheduledTasksMsg> situationQueryWrapperQueryWrapper = new QueryWrapper<>();
        situationQueryWrapperQueryWrapper.eq("module_type",moduleType);
        situationQueryWrapperQueryWrapper.eq("is_deleted",0);
        List<ScheduledTasksMsg> list1=iScheduledTasksMsgService.list(situationQueryWrapperQueryWrapper);
        if(list1!=null&&list1.size()>0){
            List<Long> longs=new ArrayList<>();
            for (ScheduledTasksMsg scheduledTasksMsg: list1) {
                longs.add(scheduledTasksMsg.getId());
            }
            if(longs!=null&&longs.size()>0){
                scheduledTasksMsgMapper.deleteByIds(longs);
            }
        }

        if(list!=null&&list.size()>0){
            for (RectificationSituation rectificationSituation: list) {
                if(rectificationSituation!=null&&rectificationSituation.getDeadlineDate()!=null){
                    if(StringUtil.isNotBlank(rectificationSituation.getRectificationSituation())&&!rectificationSituation.getRectificationSituation().equals("124001")){
                        if(StringUtil.isNotBlank(rectificationSituation.getDeadlineDate())){
                            try {
                                String dateString = rectificationSituation.getDeadlineDate();
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                Date date = sdf.parse(dateString);
                                int count=getDifferenceInDays(new Date(),date);
                                String msgCountent="";
                                if(count<=7){
                                    if(count>0){
                                        msgCountent="整改任务(单据编号："+rectificationSituation.getCode()+")距离到期还有"+count+"天，请及时整改!";
                                    }else if(count==0){
                                        msgCountent="整改任务(单据编号："+rectificationSituation.getCode()+")今天到期，请及时整改!";
                                    }else{
                                        msgCountent="整改任务(单据编号："+rectificationSituation.getCode()+")已经逾期，请及时整改!";
                                    }
                                    if(StringUtil.isNotBlank(rectificationSituation.getCompanyId())){
                                        QueryWrapper<UserOrg> userOrgQueryWrapper = new QueryWrapper<>();
                                        userOrgQueryWrapper.eq("ORGID",rectificationSituation.getCompanyId());
                                        List<UserOrg> userOrgList=iUserOrgService.list(userOrgQueryWrapper);
                                        if(userOrgList!=null&&userOrgList.size()>0){
                                            for (UserOrg userOrg: userOrgList) {

                                                PageData pageData=userDao.findbyid(userOrg.getUserId());
                                                if(pageData!=null&&StringUtil.isNotBlank(pageData.getString("isGroupAdmin"))){
                                                    if(pageData.getString("isGroupAdmin").equals("1")){
                                                        ScheduledTasksMsg scheduledTasksMsg=new ScheduledTasksMsg();
                                                        scheduledTasksMsg.setDeadlineDate(rectificationSituation.getDeadlineDate());
                                                        scheduledTasksMsg.setStatus(0);
                                                        scheduledTasksMsg.setOrgId(rectificationSituation.getCompanyId());
                                                        scheduledTasksMsg.setExternalCode(rectificationSituation.getCode());
                                                        scheduledTasksMsg.setExternalId(String.valueOf(rectificationSituation.getId()));
                                                        scheduledTasksMsg.setMsgContent(msgCountent);
                                                        scheduledTasksMsg.setUserId(pageData.getString("id"));
                                                        scheduledTasksMsg.setUserName(pageData.getString("username"));
                                                        scheduledTasksMsg.setCreateTime(new Date());
                                                        scheduledTasksMsg.setUpdateTime(new Date());
                                                        scheduledTasksMsg.setModuleType(moduleType);
                                                        scheduledTasksMsg.setRectificationSituation(rectificationSituation.getRectificationSituation());
                                                        iScheduledTasksMsgService.save(scheduledTasksMsg);
                                                    }
                                                }


                                            }

                                        }
                                    }
                                }

                            }catch (ParseException e){
                                e.printStackTrace();
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }

        }

    }
    public  int getDifferenceInDays(Date startDate, Date endDate) {
        LocalDate startLocalDate = new LocalDate(startDate);
        LocalDate endLocalDate = new LocalDate(endDate);
        return Days.daysBetween(startLocalDate, endLocalDate).getDays();
    }
}
